package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
@RequiredArgsConstructor
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

    private final AgencyCertificationAuditService agencyCertificationAuditService;

    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                                                                         .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                                                                         .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                                                                         .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 机构提交认证申请
     *
     * @param reqDTO
     */
    @Override
    @Transactional
    public void apply(AgencyCertificationAuditAddReqDTO reqDTO) {
        //从当前用户获取机构id
        Long agencyId = UserContext.currentUserId();
        //判断机构是否已经提交过认证申请
//        if (!lambdaQuery().eq(AgencyCertification::getId, agencyId).list().isEmpty()) {
//            throw new RuntimeException("该机构已经提交过认证申请");
//        }
        AgencyCertification agencyCertification = BeanUtil.copyProperties(reqDTO, AgencyCertification.class);

        //设置认证信息的主键值
        agencyCertification.setId(agencyId);

        //设置认证状态为认证中
        agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
//        插入机构认证信息
        //copy属性同步机构审核表(忽略id)
        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.copyProperties(agencyCertification, AgencyCertificationAudit.class, "id");

        //设置审核机构id
        agencyCertificationAudit.setServeProviderId(agencyId);
        save(agencyCertification);
        //插入机构认证审核信息
        agencyCertificationAuditService.save(agencyCertificationAudit);

    }

    /**
     * 查询最新的驳回原因
     *
     * @return
     */
    @Override
    public RejectReasonResDTO queryRejectReason() {
        //获取机构id
        Long agencyId = UserContext.currentUserId();
        //查询机构认证认证审核表的驳回原因
        RejectReasonResDTO reason = this.baseMapper.queryRejectReason(agencyId);
        return reason;
    }

}
